package com.linln.admin.system.web;

import com.linln.common.utils.HttpServletUtil;
import com.linln.modules.system.entity.Banners;
import com.quicks.beauty.server.vos.ActiveEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/mains/aumo")
public class AumoController extends BaseController {


    @Autowired
    private MongoTemplate mongoTemplate;


    @RequestMapping(value = "/index")
    @RequiresPermissions("mains:aumo:index")
    public ModelAndView getIndex(Integer page) {
        page = page == null ? super.pageNum : page;
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/aumo/index");
        Criteria criteria = new Criteria("phone").ne(null);

        long count = mongoTemplate.count(Query.query(criteria), ActiveEntity.class);

        Sort createTime = Sort.by(Sort.Order.desc("createTime"));
        Query query = new Query(criteria);
        Pageable request = PageRequest.of(page - 1, pageSize,createTime);
        query.with(request);
        List<ActiveEntity> objects = mongoTemplate.find(query, ActiveEntity.class);
        view.addObject("list", objects);
        Page<ActiveEntity> pages = new PageImpl<>(objects,request,count);
        view.addObject("page", pages);
        return view;
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/detail/{id}")
    public String toViewDetail(@PathVariable("id") String id, Model model) {
        Criteria criteria = new Criteria("_id").is(id);
        ActiveEntity one = mongoTemplate.findOne(Query.query(criteria), ActiveEntity.class);
        List<String> list = one.getImages().stream().filter(s -> s.length() > 6).collect(Collectors.toList());
        model.addAttribute("list", list);
        return "/mains/aumo/detail";
    }
}
